﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace IntelligentCMS
{
    public partial class CMSContentConfig : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        private List<iCMSField> listfields = new List<iCMSField>();

        public void BuildPanels()
        {


            CMSDbDataLayer fields = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            //fields.AddQuery("SELECT Field.[Name], FieldType.[Type] FROM Field INNER JOIN ContentTypeFields ON Field.ID = ContentTypeFields.FieldID WHERE ContentTypeFields.ContentTypeID = " + id);
            fields.SelectQuery = true;
            fields.Execute();

            


            for (int i = 0; i < fields.Data.Rows.Count; i++)
            {

                string fieldtype = fields.Data.Rows[i]["Type"].ToString();
                string fieldname = fields.Data.Rows[i]["Name"].ToString();
                string fieldvalue = fields.Data.Rows[i]["Value"].ToString();

                if(fieldtype == "string") {
                    CMSFieldType_string newConfig = (CMSFieldType_string)(this.LoadControl("CMSFieldType_string.ascx"));
                    Panel_Fields.Controls.Add(newConfig);
                    listfields.Add(newConfig);

                }
                else if (fieldtype == "int")
                {
                    CMSFieldType_int newConfig = (CMSFieldType_int)(this.LoadControl("CMSFieldType_int.ascx"));
                    Panel_Fields.Controls.Add(newConfig);
                    listfields.Add(newConfig);

                }
                else if (fieldtype == "file")
                {
                    CMSFieldType_file newConfig = (CMSFieldType_file)(this.LoadControl("CMSFieldType_file.ascx"));
                    Panel_Fields.Controls.Add(newConfig);
                    listfields.Add(newConfig);
                }
                else if(fieldtype == "gallery")
                {

                    /*
                    CMSDbDataLayer pluginfield = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                    pluginfield.AddQuery("SELECT [Name], [Tag], [Filename], [Type] FROM Plugin WHERE Type = 'Content'");
                    pluginfield.SelectQuerkmy = true;
                    pluginfield.Execute();

                    for (int x = 0; x < pluginfield.Data.Rows.Count; x++)
                    {
                        if (fieldtype == pluginfield.Data.Rows[x]["Name"])
                        {
                            CMSFieldType_file newConfig = (CMSFieldType_file)(this.LoadControl("CMSFieldType_file.ascx"));
                            Panel_Fields.Controls.Add(newConfig);
                        }

                    }
                     * 
                     * */

                }

                //fields.Data.Rows[i]["ID"] = newDataLayer.Data.Rows[i]["ID"].ToString();
                //fields.Data.Rows[i]["Name"] = common.FromBase64String(fields.Data.Rows[i]["Name"].ToString());
                //fields.Data.Rows[i]["Description"] = common.FromBase64String(fields.Data.Rows[i]["Description"].ToString());
                //fields.Data.Rows[i]["Edit"] = newDataLayer.Data.Rows[i]["ID"].ToString();
                

            }


            CMSDbDataLayer rolepermissions = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            //rolepermissions.AddQuery("SELECT Field.[Name], FieldType.[Name] FROM Field INNER JOIN ContentTypeFields ON Field.ID = ContentTypeFields.FieldID WHERE ContentTypeFields.ContentTypeID = " + id);
            rolepermissions.SelectQuery = true;
            rolepermissions.Execute();


            for (int i = 0; i < rolepermissions.Data.Rows.Count; i++)
            {
                string role = rolepermissions.Data.Rows[i]["Role"].ToString();
                int permissions = int.Parse(rolepermissions.Data.Rows[i]["Permissions"].ToString());

                CMSPermissionsConfig newConfig = (CMSPermissionsConfig)(this.LoadControl("CMSPermissionsConfig.ascx"));
                Panel_Permissions.Controls.Add(newConfig);
            }


            /*
            CMSDbDataLayer postable = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            postable.AddQuery("SELECT Field.[Name], FieldType.[Name] FROM Field INNER JOIN ContentTypeFields ON Field.ID = ContentTypeFields.FieldID WHERE ContentTypeFields.ContentTypeID = " + id);
            postable.SelectQuery = true;
            postable.Execute();


            for (int i = 0; i < postable.Data.Rows.Count; i++)
            {
                /*
                string role = rolepermissions.Data.Rows[i]["Role"].ToString();
                int permissions = int.Parse(rolepermissions.Data.Rows[i]["Permissions"].ToString());

                 * 
                 * 
                 * */
            /*
                string postabletype = string.Empty;
                if(postabletype == "menu") {

                }
                else if(postabletype == "gallery") {

                }
                else if(postabletype == "rss") {

                }

                //CMSPermissionsConfig newConfig = (CMSPermissionsConfig)(this.LoadControl("CMSPermissionsConfig.ascx"));
                //Panel_Permissions.Controls.Add(newConfig);
            }
            */

        }

        private long contentid = -1;
        private long contenttypeid = -1;

        /// <summary>
        /// ID of ContentType we are editing.
        /// </summary>
        public long ContentID
        {
            get
            {
                return contentid;
            }

            set
            {
                contentid = value;
            }

        }

        protected void Button_More_Click(object sender, EventArgs e)
        {

        }

        protected void Button_Close_Click(object sender, EventArgs e)
        {

        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (contentid == -1)
            {
                CMSDbDataLayer addcontent = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                //addcontent.AddQuery("INSERT INTO [ContentValues] ([ContentID], [ContentTypeID], [FieldID], [DateTime], [FieldValue]) VALUES (" + contentid + ", " + contenttypeid + ", " + fieldid + ", GETDATE())");
                addcontent.SelectQuery = false; // Insert != Select, so it's false.
                addcontent.Execute();
            }
            else
            {

                CMSDbDataLayer updatecontent = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
                //updatecontent.AddQuery("UPDATE [ContentValues] SET [ContentID] = " + contentid + ", [ContentTypeID] = " + contenttypeid + ", [FieldID] = " + fieldid + ", [DateTime] = GETDATE(), [FieldValue] = '" + fieldvalue + "'");
                updatecontent.SelectQuery = false; // Insert != Select, so it's false.
                updatecontent.Execute();

            }


        }


       
        
    }
}